package com.chuangke.admin.controller;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.chuangke.admin.constant.SysConstant;
import com.chuangke.admin.dao.mapper.SysRoleMapper;
import com.chuangke.admin.entity.SysMenu;
import com.chuangke.admin.entity.SysRole;
import com.chuangke.admin.entity.SysRoleMenu;
import com.chuangke.admin.service.SysRoleService;
import com.chuangke.admin.service.SysUserService;
import com.chuangke.common.db.page.CkPageQuery;
import com.chuangke.common.http.HttpResult;

/**
 * 角色控制器
 * 
 * @author chuangke
 * @date Oct 29, 2018
 */
@RestController
@RequestMapping("role")
public class SysRoleController {

	@Autowired
	private SysRoleService sysRoleService;
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysRoleMapper sysRoleMapper;

	@PreAuthorize("hasAuthority('sys:role:add') AND hasAuthority('sys:role:edit')")
	@PostMapping(value = "/save")
	public HttpResult save(@RequestBody SysRole record) {
		SysRole role = sysRoleService.findById(record.getId());
		if (role != null) {
			if (SysConstant.ADMIN.equalsIgnoreCase(role.getName())) {
				return HttpResult.error("超级管理员不允许修改!");
			}
		}
		// 新增角色
		if ((record.getId() != null && !"0".equals(record.getId()))) {
			SysRole sysRole = sysRoleService.findByName(record.getName()) ;
			if (sysRole != null&&!sysRole.getId().equals(record.getId())) {
				return HttpResult.error("角色名已存在!");
			}
		}
		sysRoleService.save(record) ;
		return HttpResult.ok();
	}

	@PreAuthorize("hasAuthority('sys:role:delete')")
	@PostMapping(value = "/delete")
	public HttpResult delete(@RequestBody List<SysRole> records) {
		sysRoleService.delete(records) ;
		return HttpResult.ok();
	}

	@PreAuthorize("hasAuthority('sys:role:view')")
	@PostMapping(value = "/findPage")
	public HttpResult findPage(@RequestBody Map<String,Object> params) {
		return HttpResult.ok(sysRoleService.page(new CkPageQuery(params)));
	}

	@PreAuthorize("hasAuthority('sys:role:view')")
	@GetMapping(value = "/findAll")
	public HttpResult findAll() {
		return HttpResult.ok(sysRoleService.findList());
	}

	@PreAuthorize("hasAuthority('sys:role:view')")
	@GetMapping(value = "/findRoleMenus")
	public HttpResult findRoleMenus(@RequestParam String roleId) {
		List<SysMenu> list = sysRoleService.findRoleMenus(roleId) ;
		return HttpResult.ok(list);
	}
	
	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping(value = "/findbytype")
	public HttpResult findByType(@RequestParam String type) {
		List<SysRole> list = sysRoleService.findByType(type) ;
		return HttpResult.ok(list);
	}

	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping(value="/findRoleUsers")
	public HttpResult findRoleUsers(@RequestParam String roleId) {
		return HttpResult.ok(sysUserService.findUsersByRoleId(roleId));
	}
	
	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping(value="/saveRoleUsers")
	public HttpResult saveRoleUsers(@RequestParam String roleId,@RequestParam List<String> userIds) {
		sysRoleService.saveRoleUsers(roleId,userIds);
		return HttpResult.ok();
	}
	
	@PreAuthorize("hasAuthority('sys:role:view')")
	@PostMapping(value = "/saveRoleMenus")
	public HttpResult saveRoleMenus(@RequestBody List<SysRoleMenu> records) {
		List<String> roleIdList = records.stream().map(SysRoleMenu::getRoleId).distinct().collect(Collectors.toList());
		for (String roleId : roleIdList) {
			SysRole sysRole = sysRoleMapper.selectById(roleId);
			if (SysConstant.ADMIN.equalsIgnoreCase(sysRole.getName())) {
				// 如果是超级管理员，不允许修改
				return HttpResult.error("超级管理员拥有所有菜单权限，不允许修改！");
			}
		}
		sysRoleService.saveRoleMenus(records);
		return HttpResult.ok();
	}
}
